package yy.com.yjboview.viewutil;

import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import yy.com.yjboview.R;

/**
 * @description: <描述当前版本功能>
 *
 * 这个有点难，我还没有弄懂，
 *
 * <p>
 * 简单的继承view类--基础知识
 * 引用：http://www.idtkm.com/customview/customview4/
 * </p>
 * @author: yjbo
 * @date: 2016-08-03 11:09
 */
public class SimpleViewUtil4 extends View {

    //    static Canvas mcanvas;
    //创建画笔
    static Paint mPaint = new Paint();
    //宽高
    private static int mWidth;
    private static int mHeight;
    private static int status = -1;
    Context mcontext;

    float length;
    Rect rect;
    Path mPath = new Path();
    Path mPath1 = new Path();
    Path mPath2 = new Path();
    Bitmap b;
    float r;
    public SimpleViewUtil4(Context context, int i) {
        super(context);
        mcontext = context;
        status = i;
        initPain();
        invalidate();
    }

    public SimpleViewUtil4(Context context) {
        super(context);
        mcontext = context;
        initPain();
    }

    public SimpleViewUtil4(Context context, AttributeSet attrs) {
        super(context, attrs);
        mcontext = context;
        initPain();
    }

    public SimpleViewUtil4(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mcontext = context;
        initPain();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)//21版本
    public SimpleViewUtil4(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mcontext = context;
        initPain();
    }

    private void initPain() {
        mPaint.setStyle(Paint.Style.FILL);//设置画笔类型
        mPaint.setAntiAlias(true);//抗锯齿
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    //布局1
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d("yjbo-onLayout", "getLeft==" + getLeft());
    }

    //布局1
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        size();//切割尺寸计算
        scaleBitmap();//压缩图片尺寸函数
    }

    protected void size() {
        length = Math.min(mWidth, mHeight) / 2;
        rect = new Rect(-(int) length, -(int) length, (int) length, (int) length);//绘制图片矩阵
    }

    private void scaleBitmap() {
        Drawable drawable = mcontext.getResources().getDrawable(R.drawable.icon_module);//获取图片
        if (drawable == null) {
            return;
        }
        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        if (!(drawable instanceof BitmapDrawable)) {
            return;
        }
        b = ((BitmapDrawable) drawable).getBitmap();//获取bitmap
        if (null == b) {
            return;
        }
        float scaleWidth = (float) length / b.getWidth();
        float scaleHeight = (float) length / b.getHeight();
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);//缩放矩阵
        b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);//压缩图片
    }

    //绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d("yjbo-onLayout", "getLeft==" + getLeft());
        canvas.translate(mWidth / 2, mHeight / 2);// 将画布坐标原点移动到中心位置

        if (status == 4){
            pathFigure(status);
            //绘制Path
            canvas.drawPoint(r,-r,mPaint);
            canvas.drawPath(mPath, mPaint);
        }else {
            canvas.clipPath(pathFigure(status), Region.Op.INTERSECT);//切割
            mPath.reset();
            canvas.drawBitmap(b, rect, rect, mPaint);
        }
        paintXY(canvas);
        //如果不在Canvas里面添加invalidate();那么下次调用就不会更新，
        invalidate();//所以尽量少用invalidate();方法
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    protected Path pathFigure(int i) {
        RectF rectF = new RectF();
        switch (i) {
            case 0://圆形
                mPath.addCircle(0, 0, length, Path.Direction.CW);//增加圆的path，顺时针闭合圆
                break;
            case 1://方形圆角
                rectF.left = -length;
                rectF.top = -length;
                rectF.right = length;
                rectF.bottom = length;
                mPath.addRoundRect(rectF, 40, 40, Path.Direction.CW);//圆角矩形，radius=10为圆角的半径，顺时针闭合圆角矩形
                break;
            case 2://扇形
                rectF.left = -length * 2;
                rectF.top = -length;
                rectF.right = length * 2;
                rectF.bottom = length * 3;
                mPath.moveTo(0, length);
                mPath.arcTo(rectF, 50, -50 * 2 - 180);//绘制圆弧
                break;
            case 3:
                rectF.left = -length * 2;
                rectF.top = -length;
                rectF.right = length * 2;
                rectF.bottom = length * 3;
                mPath1.moveTo(0, length);
                mPath1.arcTo(rectF, 50, -50 * 2 - 180);//较大的圆弧

                rectF.left = -length / 2;
                rectF.top = length / 2;
                rectF.right = length / 2;
                rectF.bottom = length * 3 / 2;
                mPath2.moveTo(0, length);
                mPath2.arcTo(rectF, 50, -50 * 2 - 180);//较小的圆弧

                mPath.op(mPath1, mPath2, Path.Op.XOR);//异或获取环形
                break;
            case 4:
                mPaint.setStyle(Paint.Style.STROKE);
                r = Math.min(mWidth, mHeight) * 0.6f / 2;
                rectF.left = 0;
                rectF.top = -r;
                rectF.right = r;
                rectF.bottom = 0;
                mPath.moveTo(-r,r);
                mPath.addArc(rectF, 60, 360);//从多少度开始，一共垮了多少度
                break;
        }
        return mPath;
    }

    //x,y轴
    private void paintXY(Canvas canvas) {
        mPaint.setColor(Color.BLACK);//设置画笔颜色
        mPaint.setStrokeWidth(3);
        //绘制X轴箭头
        canvas.drawLines(new float[]{
                mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f * 0.95f, -mWidth / 2 * 0.8f * 0.05f,
                mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f * 0.95f, mWidth / 2 * 0.8f * 0.05f,
                -mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f, 0
        }, mPaint);
        //绘制Y轴箭头
        canvas.drawLines(new float[]{
                -mWidth / 2 * 0.8f * 0.05f, -mHeight / 2 * 0.8f * 0.95f, 0, -mHeight / 2 * 0.8f,
                mWidth / 2 * 0.8f * 0.05f, -mHeight / 2 * 0.8f * 0.95f, 0, -mHeight / 2 * 0.8f,
                0, mHeight / 2 * 0.8f, 0, -mHeight / 2 * 0.8f
        }, mPaint);
    }

    //事件处理
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }


    public static void changeDraw(int i) {
        status = i;
    }

}
